<?php

namespace Admin\Controller;
use Think\Controller;

class UserController extends Controller {

	//广告主用户列表
    public function advertisers_user(){

        $page = I('post.page');
        $page_size = I('post.page_size');

        $user = M('user');
        $count = $user->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
    	$user = $user->order("table_user.id desc")
            ->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")
            ->join("table_channel on table_user.cid=table_channel.id")
            ->field("table_user.name,table_user.game_name,table_user.insert_time,table_channel.contacts_name,table_user.id,table_user.open_id")
            ->limit($limitStart,$page_size)
            ->select(); 
        
        $data['page']['total_page'] = $total;
        $data['list'] = $user;

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";
        $ajaxReturn['data'] = $data;
       
        $this->ajaxReturn($ajaxReturn);
    }	

    //广告主游戏用户搜索
    public function advertisers_search(){
        $user = session('user_info');
        $user_id = $user['id'];
        $game_id = I('post.game_id');
        $page = I('post.page');
        $page_size = I('post.page_size');

        if(!$user_id){
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "缺少登录人id";
            $this->ajaxReturn($ajaxReturn);
        }
        $where="1";
        if($game_id){
            $where.=" and table_user.game_id=$game_id";
        }

        $user = M('user');
        $count = $user->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")->where($where)->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        $user = $user->order("table_user.id desc")
                ->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")
                ->join("table_channel on table_user.cid=table_channel.id")
                ->field("table_user.name,table_user.game_name,table_user.insert_time,table_channel.contacts_name,table_user.id,table_user.open_id")
                ->limit($limitStart,$page_size)
                ->where($where)
                ->select(); 

        $data['page']['total_page'] = $total;
        $data['list'] = $user;

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";
        $ajaxReturn['data'] = $data;

        $this->ajaxReturn($ajaxReturn);
    }

    //渠道商用户列表
    public function channel_user(){

        $page      = I('post.page');
        $page_size = I('post.page_size');


        $where="cid=$user_id";
        $user = M('user');
        $count = $user->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        $user = $user->order("table_user.id desc")
                ->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")
                ->join("table_channel on table_user.cid=table_channel.id")
                ->field("table_user.name,table_user.game_name,table_user.insert_time,table_channel.contacts_name,table_user.id")
                ->limit($limitStart,$page_size)
                ->select(); 

        $data['page']['total_page'] = $total;
        $data['list'] = $user;

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";
        $ajaxReturn['data'] = $data;
      
        $this->ajaxReturn($ajaxReturn);
    }

    public function channel_search(){
        $user      = session('user_info');
        $user_id   = $user['id'];
        $game_id   = I('post.game_id');
        $page      = I('post.page');
        $page_size = I('post.page_size');
        if(!$user_id){
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "缺少登录人id";
            $this->ajaxReturn($ajaxReturn);
        }

        $where="cid=$user_id";
        if($game_id){
            $where.=" and game_id=$game_id";
        }

        $user = M('user');
        $count = $user->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")->where($where)->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        $user = $user->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")
                ->join("table_channel on table_user.cid=table_channel.id")
                ->field("table_user.name,table_user.game_name,table_user.insert_time,table_channel.contacts_name,table_user.id")
                ->where($where)
                ->select(); 

        $data['page']['total_page'] = $total;
        $data['list'] = $user;

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";
        $ajaxReturn['data'] = $data;
       
        $this->ajaxReturn($ajaxReturn);
    }

    //后台查看游戏用户
    public function game_user(){

        $page      = I('post.page');
        $page_size = I('post.page_size');
        $user = M('user');
        $count = $user->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")->count();

        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        $user = $user->order("table_user.id desc")
                ->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")
                ->join("table_channel on table_user.cid=table_channel.id")
                ->field("table_user.name,table_user.game_name,table_user.insert_time,table_channel.contacts_name,table_user.id")
                ->limit($limitStart,$page_size)
                ->select(); 

        $data['page']['total_page'] = $total;
        $data['list'] = $user;

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";
        $ajaxReturn['data'] = $data;

        $this->ajaxReturn($ajaxReturn);
    } 

    public function admin_search(){
        $game_id   = I('post.game_id');
        $page      = I('post.page');
        $page_size = I('post.page_size');
        $where = '';
        if($game_id){
            $where="table_user.game_id=$game_id";
        }
        $user = M('user');

        $count = $user->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")->where($where)->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        $user = $user->order("table_user.id desc")
                ->join("table_game_advertisers on table_user.game_id=table_game_advertisers.id")
                ->join("table_channel on table_user.cid=table_channel.id")
                ->field("table_user.name,table_user.game_name,table_user.insert_time,table_channel.contacts_name,table_user.id")
                ->limit($limitStart,$page_size)
                ->where($where)
                ->select(); 

        $data['page']['total_page'] = $total;
        $data['list'] = $user;

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";
        $ajaxReturn['data'] = $data;
      
        $this->ajaxReturn($ajaxReturn);
    }

 }